/**
 * Copyright 2007 Igor Chaves Cananéa
 *
 * This file is part of PBSFW
 *
 * PBSFW is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * PBSFW is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef _EVENT_QUEUE_H_
#define _EVENT_QUEUE_H_

#include "heap.h"
#include "defines.h"
#include "scheduler.h"
#include "task.h"
#include "process.h"

namespace pbsfw {
    /**
    * The event queue, implemented as a map Time->EventList. Time is
    * represented by a double and can be any unit (seconds, minutes, etc).
    */
    class TaskQueue : public Scheduler {
        private:
            Heap tasks;
            simtime_t currentTime;

        public:
            TaskQueue() : currentTime(0.0) {}
            ~TaskQueue() {
                this->reset();
            }

            /**
            * Schedules a new Event.
            * @param e Event to be scheduled.
            */
            inline void schedule(Task &t) {
                this->tasks.insert(&t);
            }


            /**
            * Unschedules an Event. The returned event is the event that was
            * in the scheduler, not the one passed as a paramater. The only
            * situation where this could happen is when you use the SAME
            * instance Event for scheduling and unscheduling.
            *
            * @param e Event to be unscheduled.
            * @return The unscheduled event. 0 if the event wasn't
            * previously scheduled.
            */
            inline Task *unschedule(Task &t) {
                return 0;
            }

            /**
            * Return the next events that must be executed at the current
            * time. If there are no more events, an empty list is returned.
            *
            * @return The events for the current time.
            */
            inline Task &getNextTask() {
                Task *t = this->tasks.remove();

                while (t->getTime() < this->currentTime) {
                    delete t;
                    t = this->tasks.remove();
                }
                this->currentTime = t->getTime();

                return *t;
            }


            /** Retrieves the current time. */
            inline simtime_t getCurrentTime() const {
                return this->currentTime;
            }

            /** Resets the event queue. */
            inline void reset() {
                this->tasks.reset();
                this->restart();
            }

            /** Restarts the event queue. */
            inline void restart() {
                this->currentTime = 0.0;
            }

            /**
            * Verifies if there are more events to be processed.
            *
            * @return true if there are more events on the queue, false
            * otherwise.
            */
            inline bool hasMoreTasks() const {
                return !this->tasks.empty();
            }

    };
};

#endif
